package com.top.art.config;

import com.top.art.interceptor.PermissionInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;
import org.springframework.web.servlet.config.annotation.*;

import javax.annotation.Resource;


/**
 * The type Web app configurer.
 *
 * @author hjh
 */
@Configuration
public class WebAppConfigurer implements WebMvcConfigurer {

    @Resource
    private PermissionInterceptor permissionInterceptor;

    /**
     * Ant path matcher path matcher.
     *
     * @return the path matcher
     */
    @Bean
    public PathMatcher antPathMatcher() {
        AntPathMatcher matcher = new AntPathMatcher();
        matcher.setCaseSensitive(false);
        return matcher;
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "DELETE", "PUT")
                .maxAge(3600 * 24);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(permissionInterceptor).addPathPatterns("/**");
    }

}
